package org.campuscontent.ui;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.campuscontent.config.Config;
import org.campuscontent.main.MainFrame;
import org.campuscontent.util.GraphInspector;

public class Template13 extends TemplateBase
{
	public static int paraID = 13;
	public String re = "";
	public JTextField textfieldReg;
	public String graphStr = "";
	public Template13(MainFrame mf)
	{
		super(mf);
		JLabel labelReg = new JLabel("Regular Expression:");
		textfieldReg = new JTextField();
		textfieldReg.setBorder(BorderFactory.createLoweredBevelBorder());

		labelReg.setBounds(30, 30, 160, 20);
		textfieldReg.setBounds(170,30,350,20);
		
		left.add(labelReg);
		left.add(textfieldReg);
	}


	public void initView()
	{
		mf.rootPanel.setDividerLocation(Config.ROOT_HEIGHT);
		this.setDividerLocation(Config.POSITION_TO_HIDE_PROBLEM_CONTROLS);
	}

	public String getAnswer()
	{
		return "";
	}
	
	public boolean setPreview()
	{
		GraphInspector gi = new GraphInspector(mf.ae.getGraph()); 
		re = gi.getRExpression();
		graphStr = mf.ae.getGraphAsString();
		if (re.length()<1)
		{
			JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "You should set the Regular Expression!");
			return false;
		} 
		else
		{
			if (!super.setPreview()) return false;
			textfieldReg.setEditable(false);
			textfieldReg.setText(re);
			mf.ae.loadGraphFromStr("");
			mf.ae.setEnable(true);
			mf.ae.toolbar.setVisible(true);
			return true;
		}
	}

	public boolean exitPreview()
	{
		textfieldReg.setEditable(true);
		mf.ae.loadGraphFromStr(graphStr);
		super.exitPreview();
		return true;
	}

	public String checkAns()
	{
		GraphInspector gi = new GraphInspector(mf.ae.getGraph()); 
		String reNow = gi.getRExpression();
		if (reNow.equals(textfieldReg.getText().trim()))
			return "True";
		else
			return "false";

	}

	public void showSolution()
	{
		super.showSolution();
		mf.ae.loadGraphFromStr("");
		mf.ae.loadGraphFromStr(graphStr);
//		GraphInspector gi = new GraphInspector(mf.ae.getGraph()); 
//		AbstractGraph ag = gi.getGraphFromRe(re);
//		mf.ae.constructGraph(ag);
	}
	
	public boolean updateToLObjectNew()
	{
//		//first check if the user have set the 
//		sAnswer = getAnswer();
//		if (sAnswer.equals("none#none#none#none#"))
//		{
//			JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "You should set the answer!");
//			return false;
//		} else
		{
			super.updateToLObjectNew();
			return true;
		}
	}
	public int getId()
	{
		return paraID;
	}
	
	public String getParaConfig()
	{
		return textfieldReg.getText().trim();
	}
	
	public void setParaConfig(String config)
	{
		this.textfieldReg.setText( config );
	}	
}
